package com.suyeer.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.suyeer.api.bean.user.TbAuthInfo;
import com.suyeer.api.service.TbAuthInfoService;
import com.suyeer.api.service.TestService;
import com.suyeer.basic.util.HttpResUtil;
import com.suyeer.common.HttpResContent;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.suyeer.common.BaseUtil.saveErrorMsgAndData;
import static com.suyeer.common.BaseUtil.saveSuccessMsgAndData;

/**
 * Created by jun on 2017/7/15.
 */
@Controller
@RequestMapping("/")
public class IndexController {

    @Reference
    TestService testService;

    @Reference
    TbAuthInfoService tbAuthInfoService;

    @RequestMapping(method = RequestMethod.GET)
    public String index() {
        return "/index";
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public void test(HttpServletRequest request, HttpServletResponse response) {
        HttpResContent<String> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("测试数据");
        try {
            long t0 = System.currentTimeMillis();
            System.err.println(testService.getTest());
            long t1 = System.currentTimeMillis();
            test(false);
            long t2 = System.currentTimeMillis();
            System.err.println(t1-t0);
            System.err.println(t2-t1);
            System.err.println(t2-t0);
            saveSuccessMsgAndData(hrc, hrc.getSessionId());
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc);
        }
    }

    private void test(boolean flag) throws Exception {
        TbAuthInfo tbAuthInfo = new TbAuthInfo();
        tbAuthInfo.setAuthPoint("ccc");
        tbAuthInfo.setAuthUri("/23424");
        tbAuthInfoService.addTbAuthInfo(tbAuthInfo);
        tbAuthInfo.setAuthUri("/66666");
        if (flag) {
            throw new Exception("抛出异常");
        }
//        tbAuthInfoService.addTbAuthInfo(tbAuthInfo);
    }

    @RequestMapping(value = "/{page}", method = RequestMethod.GET)
    public String page(HttpServletRequest request) {
        return request.getRequestURI();
    }


}
